package rad.framework.mda.jpagen.actions;

import rad.framework.mda.generator.Console;
import rad.framework.mda.generator.util.StereotypeHelper;
import rad.framework.mda.util.ModelUtil;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * @author boudyacho
 * 
 */
public class AddColumnActionExtension extends BaseUtiltiyActionExtension {

	protected static final String COLUMN = "javax.persistence.Column";

	public void fillContextMenu(IMenuManager aMenu) {
		getSubMenu(aMenu).add(new AddColumnAction());
	}

	protected void execute(Class clazz) {
		if (StereotypeHelper
				.isStereotypeApplied(clazz, StereotypeHelper.ENTITY)) {
			EList<NamedElement> list = clazz.getMembers();
			for (NamedElement namedElement : list) {
				if (namedElement instanceof Property) {
					execute((Property) namedElement);
				}
			}
		}
	}

	protected void execute(Property property) {
		if (ModelUtil.isStereotypeApplied(property, O2M)
				|| ModelUtil.isStereotypeApplied(property, M2M)
				|| ModelUtil.isStereotypeApplied(property, M2O)
				|| ModelUtil.isStereotypeApplied(property, O2O)
				|| ModelUtil.isStereotypeApplied(property, EMBEDDED)) {
			return;
		} else {
			applyColumn(property);
			Console.getStream().println(
					"Added " + COLUMN + " - " + property.getQualifiedName());
		}
	}

	protected void applyColumn(Property element) {
		Stereotype stereotype = (Stereotype) ModelUtil.getProfileElement(
				element, COLUMN);
		if (stereotype != null) {
			if (!ModelUtil.isStereotypeApplied(element, COLUMN)) {
				element.applyStereotype(stereotype);
			}
			if (ModelUtil.isStereotypeApplied(element, "javax.persistence.Id")) {
				element.setValue(stereotype, "name", NameUtil
						.getAbbreviation((NamedElement) element.getOwner())
						+ "_ID");
			} else {
				element.setValue(stereotype, "name", NameUtil
						.getAbbreviation(element));
			}
			if ("String".equals(element.getType().getName())) {
				if (element.getValue(stereotype, "length").equals(
						new Integer(0))) {
					element.setValue(stereotype, "length", 255);
				}
			}
			// TODO other properties
			// length=0
			// unique=false
			// precision=0
			// scale=0
			// nullable=true
			// insertable=true
			// updatable=true

			/*
			 * 
			 * @Column( name="columnName"; (1) boolean unique() default false;
			 * (2) boolean nullable() default true; (3) boolean insertable()
			 * default true; (4) boolean updatable() default true; (5) String
			 * columnDefinition() default ""; (6) String table() default ""; (7)
			 * int length() default 255; (8) int precision() default 0; //
			 * decimal precision (9) int scale() default 0; // decimal scale
			 * 
			 * 
			 */

			// TODO other stereotypes
			// @Id
		}
	}

	private class AddColumnAction extends BaseUtilityAction {
		private static final String LABEL = "Add Column Stereotypes";

		public AddColumnAction() {
			super(LABEL);
		}
	}
}
